package com.lonely.order.support.detail.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.lonely.common.core.redis.RedisCache;
import com.lonely.goods.domain.dto.goods.*;
import com.lonely.goods.domain.vo.goods.GoodsQuotationSheetTotalCastVO;
import com.lonely.goods.enums.GoodsEnum;
import com.lonely.order.domain.dto.api.*;
import com.lonely.order.domain.entity.SysOrder;
import com.lonely.order.domain.entity.SysSheinOpenApiSecret;
import com.lonely.order.domain.vo.order.OrderDetailVO;
import com.lonely.order.domain.vo.split.GetSplitFactoryNameVO;
import com.lonely.order.service.IMesApiService;
import com.lonely.order.service.ISysSheinOpenApiSecretService;
import com.lonely.order.support.detail.GetOrderDetailContext;
import com.lonely.system.service.ISysFactoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author : 黄志标
 * @date: 2025/7/14 13:54
 * @description: 基于shein接口获取订单明细
 */
@Service
public class GetOrderDetailFromShein extends AbstractGetOrderDetail {

    @Autowired
    private IMesApiService mesApiService;

    @Autowired
    private ISysSheinOpenApiSecretService sheinOpenApiSecretService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysFactoryService factoryService;

    /**
     * 获取订单明细
     *
     * @param sysOrder
     * @return
     */
    @Override
    public OrderDetailVO getOrderDetail(SysOrder sysOrder) {
        // 先尝试从redis中获取，如果获取不到，则调用接口获取
        String key = String.format("order:detail:%s", sysOrder.getProduceOrderId());
        OrderDetailVO cacheOrderDetail = this.redisCache.getCacheObject(key);
        if (cacheOrderDetail != null) {
            return cacheOrderDetail;
        }

        // 组装数据
        GetOrderDetailContext getOrderDetailContext = this.buildContext(sysOrder);
        QueryProduceOrderInfoByIdResDTO orderInfoById = getOrderDetailContext.getOrderInfoById();

        OrderDetailVO orderDetailVO = new OrderDetailVO();
        BeanUtils.copyProperties(sysOrder, orderDetailVO);
        orderDetailVO.setFactoryName(this.factoryService.getFactoryName(sysOrder.getFactoryId()));
        orderDetailVO.setProduceOrderId(StrUtil.toString(sysOrder.getProduceOrderId()));

        orderDetailVO.setAcceptOrderTime(orderInfoById.getAcceptOrderTime());
        orderDetailVO.setDeliveryTime(DateUtil.formatDateTime(orderInfoById.getDeliveryTime()));
        orderDetailVO.setDesignCode(orderInfoById.getDesignCode());
        orderDetailVO.setOrderPrice(orderInfoById.getOrderPrice());
        orderDetailVO.setOrderType(orderInfoById.getOrderType());
        orderDetailVO.setPlaceOrderTime(DateUtil.formatDate(orderInfoById.getPlaceOrderTime()));
        orderDetailVO.setQuantity(orderInfoById.getQuantity());
        orderDetailVO.setRefSku(orderInfoById.getRefSku());
        orderDetailVO.setColor(orderInfoById.getColors());
        orderDetailVO.setMultiPiecesQuantity(orderInfoById.getMultiPiecesQuantity());
        orderDetailVO.setTags(super.extractOrderTags(sysOrder));

        // 设置分单工厂相关信息
        GetSplitFactoryNameVO splitFactoryNameInfo = super.getSplitFactoryNameInfo(sysOrder.getId());
        if (splitFactoryNameInfo != null) {
            orderDetailVO.setWorkshopSplitFactoryName(splitFactoryNameInfo.getWorkshopFactoryName());
            orderDetailVO.setTailSplitFactoryName(splitFactoryNameInfo.getTailFactoryName());
        }

        // 提取部件信息
        List<GoodsQuotationSheetPartDTO> partDTOS = extractParts(getOrderDetailContext);
        orderDetailVO.setParts(partDTOS);
        orderDetailVO.setTotalCasts(this.extractTotalCasts(getOrderDetailContext, partDTOS));

        // 添加到缓存中，10分钟过期
        this.redisCache.setCacheObject(key, orderDetailVO, 10, TimeUnit.MINUTES);

        return orderDetailVO;
    }


    /**
     * 计算成本总计
     *
     * @param detailContext
     * @return
     */
    private List<GoodsQuotationSheetTotalCastVO> extractTotalCasts(GetOrderDetailContext detailContext, List<GoodsQuotationSheetPartDTO> partDTOS) {
        // 成本统计
        List<GoodsQuotationSheetTotalCastVO> totalCasts = new ArrayList<>();
        List<GoodsQuotationSheetTotalCastVO> partTotalCasts = new ArrayList<>();
        for (GoodsQuotationSheetPartDTO currentPart : partDTOS) {

            // 当前部件的当前颜色的物料总和
            for (GoodsQuotationSheetPartMaterialDTO currentPartColorMaterial : currentPart.getPartMaterials()) {
                // 当前部件的成本统计
                GoodsQuotationSheetTotalCastVO currentPartTotalCast = new GoodsQuotationSheetTotalCastVO();
                currentPartTotalCast.setType(GoodsEnum.GoodsTotalCostTypeEnum.PART.getType());
                currentPartTotalCast.setPartId(currentPart.getPartId());
                currentPartTotalCast.setPartColor(currentPartColorMaterial.getGoodsColor());

                // 当前部件的加工费用
                BigDecimal processingCost;
                if (currentPart.getPartProcess() == null || StrUtil.isBlank(currentPart.getPartProcess().getTotalProcessCosts())) {
                    processingCost = new BigDecimal(0);
                } else {
                    processingCost = new BigDecimal(currentPart.getPartProcess().getTotalProcessCosts());
                }
                processingCost = NumberUtil.round(processingCost, 2);

                // 当前部件的物料金额总和
                BigDecimal materialCost = currentPartColorMaterial.getMaterials().stream()
                        .map(x -> {
                            if (StrUtil.isBlank(x.getAmount())) {
                                return new BigDecimal(0);
                            }
                            return new BigDecimal(x.getAmount());
                        })
                        .reduce(new BigDecimal(0), BigDecimal::add);
                materialCost = NumberUtil.round(materialCost, 2);

                // 总价
                BigDecimal totalPrice = NumberUtil.round(NumberUtil.add(processingCost, materialCost), 2);

                currentPartTotalCast.setTotalPrice(totalPrice.toPlainString());
                currentPartTotalCast.setMaterialCost(materialCost.toPlainString());
                currentPartTotalCast.setProcessingCost(processingCost.toPlainString());
                currentPartTotalCast.setOtherExpenses(null);
                currentPartTotalCast.setPartName(String.format("%s(%s)", currentPart.getPartLibrary(), currentPartColorMaterial.getGoodsColor()));

                partTotalCasts.add(currentPartTotalCast);
                totalCasts.add(currentPartTotalCast);
            }
        }


        // 处理总计
        LinkedHashMap<Long, List<GoodsQuotationSheetTotalCastVO>> partTotalCastMap = partTotalCasts.stream()
                .collect(Collectors.groupingBy(GoodsQuotationSheetTotalCastVO::getPartId, LinkedHashMap::new,
                        Collectors.toList()));

        Map<Long, String> partIdNameMap = detailContext.getPartIdNameMap();

        partTotalCastMap.forEach((partId, partTotalCastList) -> {
            GoodsQuotationSheetTotalCastVO currentPartTotalCast = new GoodsQuotationSheetTotalCastVO();
            currentPartTotalCast.setType(GoodsEnum.GoodsTotalCostTypeEnum.TOTAL_COST.getType());
            currentPartTotalCast.setPartId(partId);
            currentPartTotalCast.setPartColor(null);
            currentPartTotalCast.setPartName(String.format("%s-总价", MapUtil.getStr(partIdNameMap, partId, "默认部件")));

            BigDecimal totalPrice = partTotalCastList.stream().map(x -> new BigDecimal(x.getTotalPrice()))
                    .reduce(new BigDecimal(0), BigDecimal::add);
            totalPrice = NumberUtil.round(totalPrice, 2);
            currentPartTotalCast.setTotalPrice(totalPrice.toPlainString());

            BigDecimal materialCost = partTotalCastList.stream().map(x -> new BigDecimal(x.getMaterialCost()))
                    .reduce(new BigDecimal(0), BigDecimal::add);
            materialCost = NumberUtil.round(materialCost, 2);
            currentPartTotalCast.setMaterialCost(materialCost.toPlainString());

            BigDecimal processingCost = partTotalCastList.stream().map(x -> new BigDecimal(x.getProcessingCost()))
                    .reduce(new BigDecimal(0), BigDecimal::add);
            processingCost = NumberUtil.round(processingCost, 2);
            currentPartTotalCast.setProcessingCost(processingCost.toPlainString());
            currentPartTotalCast.setOtherExpenses(null);
            totalCasts.add(currentPartTotalCast);
        });

        return totalCasts;
    }


    /**
     * 构建上下文
     *
     * @param sysOrder
     * @return
     */
    private GetOrderDetailContext buildContext(SysOrder sysOrder) {

        GetOrderDetailContext context = new GetOrderDetailContext();

        //1. 获取该订单对应的 openapi密钥信息
        SysSheinOpenApiSecret openApiSecret = this.sheinOpenApiSecretService.getByFactoryId(sysOrder.getFactoryId());
        Assert.notNull(openApiSecret, "该工厂未配置密钥信息");
        context.setOpenApiSecret(openApiSecret);

        // 2. 获取根据订单id获取订单详情接口数据
        QueryProduceOrderInfoByIdReqDTO reqDTO = new QueryProduceOrderInfoByIdReqDTO();
        reqDTO.setProduceOrderId(sysOrder.getProduceOrderId());
        QueryProduceOrderInfoByIdResDTO produceOrderInfoByIdResDTO = this.mesApiService.queryProduceOrderInfoById(openApiSecret, reqDTO);
        context.setOrderInfoById(produceOrderInfoByIdResDTO);

        //3. 获取大货BOM数据
        GetBigGoodsBomReqDTO getBigGoodsBomReqDTO = new GetBigGoodsBomReqDTO();
        getBigGoodsBomReqDTO.setProduceOrderId(Convert.toInt(sysOrder.getProduceOrderId()));
        GetBigGoodsBomResDTO bigGoodsBom = this.mesApiService.getBigGoodsBom(openApiSecret, getBigGoodsBomReqDTO);
        context.setBigGoodsBom(bigGoodsBom);

        // 4. 是否是套装
        boolean isSuit = bigGoodsBom.getBigGoodsBom().stream().anyMatch(x -> x.getComponentId() != null);
        context.setSuit(isSuit);

        // 5. 部件与名称的映射
        Map<Long, String> partIdNameMap = bigGoodsBom.getBigGoodsBom().stream().filter(x -> x.getComponentId() != null)
                .collect(Collectors.toMap(GetBigGoodsBomResDTO.BigGoodsBom::getComponentId, GetBigGoodsBomResDTO.BigGoodsBom::getPartName, (a, b) -> b));
        context.setPartIdNameMap(partIdNameMap);


        // 6. 获取对应的面辅料二次工艺接口数据
//        GetSecondPrecessReqDTO getSecondPrecessReqDTO = new GetSecondPrecessReqDTO();
//        getSecondPrecessReqDTO.setProduceOrderIdList(Lists.newArrayList(Convert.toInt(sysOrder.getProduceOrderId())));
//        List<GetSecondPrecessResDTO> secondPrecesses = this.mesApiService.getSecondPrecess(openApiSecret, getSecondPrecessReqDTO);
//        GetSecondPrecessResDTO getSecondPrecessResDTO = secondPrecesses.stream().filter(x -> Objects.equals(x.getProduceOrderId(), Convert.toInt(sysOrder.getProduceOrderId()))).findFirst().orElse(null);
//        context.setSecondPrecess(getSecondPrecessResDTO);

        return context;
    }


    /**
     * 提取部件信息
     *
     * @param detailContext
     * @return
     */
    private List<GoodsQuotationSheetPartDTO> extractParts(GetOrderDetailContext detailContext) {
        if (detailContext.isSuit()) {
            // 是套装
            List<Long> partIds = detailContext.getBigGoodsBom().getBigGoodsBom().stream()
                    .map(GetBigGoodsBomResDTO.BigGoodsBom::getComponentId)
                    .filter(Objects::nonNull)
                    .distinct()
                    .collect(Collectors.toList());
            List<GoodsQuotationSheetPartDTO> result = new ArrayList<>();
            for (Long partId : partIds) {
                GoodsQuotationSheetPartDTO partDTO = new GoodsQuotationSheetPartDTO();
                partDTO.setPartId(partId);
                partDTO.setPartLibrary(MapUtil.getStr(detailContext.getPartIdNameMap(), partId));
                partDTO.setPartMaterials(extractPartMaterials(detailContext, partId));
                partDTO.setPartSecondProcesses(this.extractPartSecondProcesses(detailContext, partId));
                partDTO.setPartReadyToWearSecondProcesses(this.extractPartSecondProcessesInstance(detailContext, partId));
                partDTO.setPartProcess(this.extractPartProcess(detailContext, partId));
                result.add(partDTO);
            }
            return result;
        } else {
            // 非套装
            GoodsQuotationSheetPartDTO partDTO = new GoodsQuotationSheetPartDTO();
            partDTO.setPartId(0L);
            partDTO.setPartLibrary("默认部件");
            partDTO.setPartMaterials(extractPartMaterials(detailContext, null));
            partDTO.setPartSecondProcesses(this.extractPartSecondProcesses(detailContext, null));
            partDTO.setPartReadyToWearSecondProcesses(this.extractPartSecondProcessesInstance(detailContext, null));
            partDTO.setPartProcess(this.extractPartProcess(detailContext, null));
            return Lists.newArrayList(partDTO);
        }
    }


    /**
     * 提取工序数据
     *
     * @param detailContext
     * @return
     */
    private GoodsQuotationSheetPartProcessDTO extractPartProcess(GetOrderDetailContext detailContext, Long partId) {

        QueryProduceOrderInfoByIdResDTO resDTO = detailContext.getOrderInfoById();

        // 获取工序费用信息
        ClothCostFeeDetail processCost = resDTO.getClothCostFeeDetail().stream().filter(x -> Objects.equals("加工费", x.getCategory())).findFirst().orElse(null);
        if (processCost == null || CollUtil.isEmpty(processCost.getClothCostFeeDetail())) {
            GoodsQuotationSheetPartProcessDTO partProcessDTO = new GoodsQuotationSheetPartProcessDTO();
            partProcessDTO.setTotalProcessCosts(null);
            partProcessDTO.setProfit(null);
            partProcessDTO.setProcessingFee(null);
            if (processCost != null) {
                partProcessDTO.setMagnification(StrUtil.toString(processCost.getScale()));
            }
            partProcessDTO.setTotalLaborCost(null);
            partProcessDTO.setRemark(null);
            partProcessDTO.setProcessItems(new ArrayList<>());
            return partProcessDTO;
        }

        List<GoodsQuotationSheetPartProcessDTO.ProcessItemVO> processItemVOS = processCost.getClothCostFeeDetail().stream().filter(x -> Objects.equals(x.getComponentId(), partId)).map(currentProcess -> {
            GoodsQuotationSheetPartProcessDTO.ProcessItemVO processItemVO = new GoodsQuotationSheetPartProcessDTO.ProcessItemVO();
            processItemVO.setId(null);
            processItemVO.setProcessName(currentProcess.getTitle());
            processItemVO.setWage(StrUtil.toString(currentProcess.getUnitPrice()));
            processItemVO.setRemark(null);
            return processItemVO;
        }).collect(Collectors.toList());

        // 计算工价合计
        BigDecimal totalLaborCost = processItemVOS.stream().map(x -> new BigDecimal(x.getWage())).reduce(BigDecimal.ZERO, BigDecimal::add);

        // 倍率
        BigDecimal magnification = BigDecimal.valueOf(NumberUtil.nullToZero(processCost.getScale()));

        // 计算利润率
        double profitMargin = processCost.getClothCostFeeDetail().get(0).getProfitRate();
//        double profitMargin = NumberUtil.div(NumberUtil.sub(processCost.getSumCostTotal(), processCost.getSumCostForSale()), processCost.getSumCostForSale().doubleValue());

        // 利润率百分比
        double profitMarginPercentage = NumberUtil.mul(profitMargin, 100);

        // 计算加工费
        BigDecimal processingFee = NumberUtil.mul(totalLaborCost, magnification);

        // 计算工序费用合计
        BigDecimal totalProcessCosts = NumberUtil.mul(processingFee, NumberUtil.add(new BigDecimal(1), profitMargin));

        GoodsQuotationSheetPartProcessDTO partProcessDTO = new GoodsQuotationSheetPartProcessDTO();
        partProcessDTO.setTotalProcessCosts(NumberUtil.round(totalProcessCosts, 2).toPlainString());
        partProcessDTO.setProfit(NumberUtil.round(profitMarginPercentage, 2).toPlainString());
        partProcessDTO.setProcessingFee(NumberUtil.round(processingFee, 2).toPlainString());
        partProcessDTO.setMagnification(StrUtil.toString(processCost.getScale()));
        partProcessDTO.setTotalLaborCost(NumberUtil.round(totalLaborCost, 2).toPlainString());
        partProcessDTO.setRemark(null);
        partProcessDTO.setProcessItems(processItemVOS);

        return partProcessDTO;
    }


    /**
     * 提取成衣二次工艺信息
     *
     * @param detailContext
     * @return
     */
    private List<GoodsQuotationSheetPartSecondProcessDTO> extractPartSecondProcessesInstance(GetOrderDetailContext detailContext, Long partId) {
        QueryProduceOrderInfoByIdResDTO resDTO = detailContext.getOrderInfoById();

        // 获取二次工艺信息
        ClothCostFeeDetail secondProcessInstanceCost = resDTO.getClothCostFeeDetail().stream().filter(x -> Objects.equals("成衣二次工艺", x.getCategory())).findFirst().orElse(null);
        if (secondProcessInstanceCost == null) {
            return new ArrayList<>();
        }
        return this.batchConvertSecondProcessItem2PartSecondProcess(secondProcessInstanceCost.getClothCostFeeDetail(), partId);
    }


    /**
     * 提取面辅料二次工艺信息
     *
     * @param detailContext
     * @return
     */
    private List<GoodsQuotationSheetPartSecondProcessDTO> extractPartSecondProcesses(GetOrderDetailContext detailContext, Long partId) {
        QueryProduceOrderInfoByIdResDTO resDTO = detailContext.getOrderInfoById();

        // 获取二次工艺信息
        ClothCostFeeDetail secondProcessCost = resDTO.getClothCostFeeDetail().stream().filter(x -> Objects.equals("面辅料二次工艺", x.getCategory())).findFirst().orElse(null);
        if (secondProcessCost == null || CollUtil.isEmpty(secondProcessCost.getClothCostFeeDetail())) {
            return new ArrayList<>();
        }

        return this.batchConvertSecondProcessItem2PartSecondProcess(secondProcessCost.getClothCostFeeDetail(), partId);
    }

    /**
     * 批量将二次工艺信息转换成 GoodsQuotationSheetPartSecondProcessDTO 对象
     *
     * @param clothCostFeeItems
     * @param partId
     * @return
     */
    private List<GoodsQuotationSheetPartSecondProcessDTO> batchConvertSecondProcessItem2PartSecondProcess(List<ClothCostFeeItem> clothCostFeeItems, Long partId) {
        List<GoodsQuotationSheetPartSecondProcessDTO> result = new ArrayList<>();
        for (ClothCostFeeItem currentSecondProcess : clothCostFeeItems) {
            // 判断部件id是否匹配
            if (!Objects.equals(partId, currentSecondProcess.getComponentId())) {
                continue;
            }

            GoodsQuotationSheetPartSecondProcessDTO partSecondProcessDTO = new GoodsQuotationSheetPartSecondProcessDTO();
            partSecondProcessDTO.setId(null);
            partSecondProcessDTO.setUnitPrice(Convert.toStr(currentSecondProcess.getUnitPrice()));
            partSecondProcessDTO.setSimpleUse(currentSecondProcess.getSimpleUse());
            partSecondProcessDTO.setAmount(StrUtil.toString(currentSecondProcess.getPrice()));

            if (StrUtil.isNotBlank(currentSecondProcess.getTitle())) {
                int startIndex = currentSecondProcess.getTitle().indexOf("/");
                partSecondProcessDTO.setProcessName(currentSecondProcess.getTitle().substring(0, startIndex));
                partSecondProcessDTO.setSupplierName(currentSecondProcess.getTitle().substring(startIndex + 1));
            }
            result.add(partSecondProcessDTO);
        }
        return result;
    }

    /**
     * 提取指定部件对应的物料信息
     *
     * @param getOrderDetailContext
     * @param partId
     * @return
     */
    private List<GoodsQuotationSheetPartMaterialDTO> extractPartMaterials(GetOrderDetailContext getOrderDetailContext, Long partId) {

        QueryProduceOrderInfoByIdResDTO resDTO = getOrderDetailContext.getOrderInfoById();

        // 获取对应的大货BOM数据
        GetBigGoodsBomResDTO bigGoodsBom = getOrderDetailContext.getBigGoodsBom();

        // 结合物料明细费用中的详情以及大货bom数据，组装物料信息数据
        Map<Integer, GetBigGoodsBomResDTO.BigGoodsBom> bigGoodsBomMap = new HashMap<>();
        for (int i = 0; i < bigGoodsBom.getBigGoodsBom().size(); i++) {
            bigGoodsBomMap.put(i, bigGoodsBom.getBigGoodsBom().get(i));
        }

        // 物料费用相关数据
        ClothCostFeeDetail materialCost = resDTO.getClothCostFeeDetail().stream().filter(x -> Objects.equals("物料费", x.getCategory())).findFirst().orElse(null);
        Map<Integer, ClothCostFeeItem> materialItemCostMap = new HashMap<>();
        if (materialCost != null) {
            // 物料费用map
            for (int i = 0; i < materialCost.getClothCostFeeDetail().size(); i++) {
                materialItemCostMap.put(i, materialCost.getClothCostFeeDetail().get(i));
            }

        }

        // 物料供应商相关数据
        Map<String, PurchaseInfo> skuPurchaseMap = resDTO.getPurchaseInfo().stream().collect(Collectors.toMap(PurchaseInfo::getMaterialSku, Function.identity()));

        List<GoodsMaterialItemDTO> materialItems = new ArrayList<>();
        for (int i = 0; i < bigGoodsBom.getBigGoodsBom().size(); i++) {
            GetBigGoodsBomResDTO.BigGoodsBom currentBigGoodsBom = bigGoodsBomMap.get(i);

            // 判断部件id是否匹配
            if (!Objects.equals(partId, currentBigGoodsBom.getComponentId())) {
                continue;
            }

            GoodsMaterialItemDTO materialItemDTO = new GoodsMaterialItemDTO();
            materialItemDTO.setGoodsPartMaterialId(null);
            materialItemDTO.setGoodsPartId(null);
            materialItemDTO.setMaterialId(null);
            materialItemDTO.setMaterialItemId(null);
            materialItemDTO.setGoodsColor(resDTO.getColors());
            materialItemDTO.setItem(currentBigGoodsBom.getMaterialType());
            materialItemDTO.setSku(currentBigGoodsBom.getMaterialSku());
            materialItemDTO.setMaterialName(currentBigGoodsBom.getMaterialName());
            materialItemDTO.setUnit(currentBigGoodsBom.getUnit());
            materialItemDTO.setColor(currentBigGoodsBom.getColor());
            materialItemDTO.setBorderedDoorWidth(currentBigGoodsBom.getWidth());
            materialItemDTO.setGramWeight(currentBigGoodsBom.getWeight());
            materialItemDTO.setIngredients(null);
            materialItemDTO.setTypeName(currentBigGoodsBom.getFirstClass());
            materialItemDTO.setStraightBullet(null);
            materialItemDTO.setHorizontalBullet(null);
            materialItemDTO.setPic(null);
            materialItemDTO.setPartUsed(currentBigGoodsBom.getPart());
            materialItemDTO.setSinglePieceUsage(currentBigGoodsBom.getOneSimpleUse());
            if (StrUtil.isNotBlank(currentBigGoodsBom.getProcess())) {
                materialItemDTO.setSecondProcess(Lists.newArrayList(currentBigGoodsBom.getProcess()));
            }
            materialItemDTO.setLoss(currentBigGoodsBom.getSupplierLoss());

            // 处理价格部分
            if (materialItemCostMap.containsKey(i)) {
                ClothCostFeeItem clothCostFeeItem = materialItemCostMap.get(i);
                materialItemDTO.setUnitPrice(StrUtil.toString(clothCostFeeItem.getUnitPrice()));
                materialItemDTO.setAmount(StrUtil.toString(clothCostFeeItem.getPrice()));
                materialItemDTO.setSupplierProfitMargin(clothCostFeeItem.getSupplierProfit());
                materialItemDTO.setMrpTaxInclusiveUnitPrice(null);
                materialItemDTO.setSupplierUnitPrice(null);
            }

            // 处理供应商部分
            if (skuPurchaseMap.containsKey(currentBigGoodsBom.getMaterialSku())) {
                PurchaseInfo purchaseInfo = skuPurchaseMap.get(currentBigGoodsBom.getMaterialSku());
                materialItemDTO.setSupplierId(null);
                materialItemDTO.setSupplierName(purchaseInfo.getSupplierName());
                materialItemDTO.setSupplierAddress(purchaseInfo.getSupplierAddr());
                materialItemDTO.setSupplierPhone(purchaseInfo.getSupplierPhone());
                materialItemDTO.setSupplierCode(purchaseInfo.getSupplierCode());
                materialItemDTO.setSupplierColorCode(purchaseInfo.getSupplierColorNum());
            }


            // 不使用的字段
            materialItemDTO.setFlowerModel(null);
            materialItemDTO.setFlowerPatternPic(null);
            materialItemDTO.setCuttingMethod(null);
            materialItemDTO.setRemark(null);

            materialItems.add(materialItemDTO);
        }

        List<GoodsQuotationSheetPartMaterialDTO> result = new ArrayList<>();
        GoodsQuotationSheetPartMaterialDTO partMaterialDTO = new GoodsQuotationSheetPartMaterialDTO();
        partMaterialDTO.setGoodsPartId(partId);
        partMaterialDTO.setGoodsColor(resDTO.getColors());
        partMaterialDTO.setMaterials(materialItems);
        result.add(partMaterialDTO);
        return result;
    }


}
